<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;//导入数据库类(Db类)


class User extends Controller
{
	//用户页面遍历出所有用户
    public function index()
    {
 
        // 使用Db类
        $list = Db::table('boke_user')->select();
		$this->assign('list',$list);
	      // return $this->fetch();
		return view('user/user');
    }

    //跳转修改用户界面
    public function adit($id)
    {
    	// 使用Db类
        $list = Db::table('boke_user')->select($id);
		$this->assign('list',$list);
	      // return $this->fetch();
   
    	return view('user/adit');
    }


  
  //跳转添加用户界面
    public function add()
    {
    	// $list = Db::table('boke_user')->select();
    	// $this->assign('list',$list);
    	return view('user/add');
    }

      //执行添加操作
    public function insert()
    {
    	// var_dump($_POST);
    		//判断表单是否为空
			foreach($_POST as $v){
		 			if($v == ''){
		 				$this->error('表单未填写完整');
		 				exit;
		 			}
			}
    		//拼接字段
			$field_key = '';
			$field_value = '';
			foreach($_POST as $k => $v){
				if($k == 'pwd'){
					$v = md5($v);
				}

				if($k != 'repwd'){
					$field_key .= '`'.$k.'`,';
					$field_value .= '"'.$v.'",';
				}
			}
			$field_key .= '`regTime`';
			$field_value .= time();
			// echo $field_value;

			if(Db::execute('insert into boke_user('.$field_key.') values('.$field_value.')'))
				{
					$this->success('新增成功', 'User/index');
				}else{
					$this->success('你注册的手机号码已被注册');
					exit;
				}
    }

     

    
     //执行删除操作
	public function del($id)
	{
		var_dump($id);
		//判断有无传递ID
		if (empty($id)) {
			$this->error('删除失败', 'User/index','',3);
			exit;
		}
		if(Db::table('boke_user')->delete($id))
		{
			$this->success('删除成功','user/index');
		}else{
			$this->error('删除失败');
			exit;
		}
	}

	// 执行修改
	public  function  update()
	{
		//判断表单是否为空
			foreach($_POST as $v){
		 			if($v == ''){
		 				$this->error('表单未填写完整');
		 				exit;
		 			}
			}
		$id = $_POST['id'];
		$nickname = $_POST['nickname'];
		$tel = $_POST['tel'];
		$email = $_POST['email'];
		

		if(Db::table('boke_user')->where('id',$id)->update(['nickname' => $nickname,'tel' => $tel,'email' => $email])){
			$this->success('修改成功','user/index');
		}else{
			$this->error('修改失败');
		}
		
	
	}
}

